package com.example.jianbing.juc;

import sun.java2d.pipe.SpanIterator;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @ClassName : CallableDemo
 * @Description :
 * @Author : JB
 * @Date: 2020-08-06 02:30
 */


class MyThread implements  Runnable{
    @Override
    public void run() {

    }
}

class  MyThread2 implements Callable<Integer>{

    @Override
    public Integer call() throws Exception {
        System.out.println("Callable");
        return 1024;
    }
}

/***
 *  1. Runnable 与 Callable 的不同
 *      1.实现的方法名字不同
 *      2.Callable需抛出异常
 *      3.Callable有返回值
 */

/***
 *
 * 多线程，第3种获得多线程的方式
 */
public class CallableDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask futureTask = new FutureTask(new MyThread2());
        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start();

        System.out.println(Thread.currentThread().getName()+"***计算完成");

        System.out.println(futureTask.get());  //放最后，避免阻塞
    }
}
